package com.xqboss.apps.controller.system;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.xqboss.apps.convert.system.SysPopupConverter;
import com.xqboss.apps.domain.sys.SysPopup;
import com.xqboss.apps.dto.system.AppGetPopupListDto;
import com.xqboss.apps.service.sys.SysPopupService;
import com.xqboss.apps.vo.sys.AppPopupVo;
import com.xqboss.common.annotation.Anonymous;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.core.domain.model.LoginUser;
import com.xqboss.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"弹窗管理"})
@Slf4j
@RestController
@RequestMapping("/popup")
public class SysPopupController extends BaseController {

    @Autowired
    private SysPopupService popupService;

    @Autowired
    private SysPopupConverter popupConverter;

    @Anonymous
    @ApiOperation("获取弹窗列表")
    @GetMapping("list")
    public R<List<AppPopupVo>> list(@Validated AppGetPopupListDto params) {
        LoginUser loginUser = SecurityUtils.getLoginUserOrNull();
        if(loginUser != null){
            params.setUserId(loginUser.getUserId());
            params.setRegisterTime(LocalDateTimeUtil.of(loginUser.getUser().getCreateTime()));
        }
        List<SysPopup> list = popupService.getSaleablePopupList(params);
        List<AppPopupVo> voList = popupConverter.toAppPopupVoList(list);
        return R.ok(voList);
    }

}
